﻿#region using...
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Infusion.Silverlight.Controls.Ribbon;
#endregion

namespace Infusion.Silverlight.Controls.Input
{
    #region Delegates...
    public delegate void CheckedEventHandler(object sender, EventArgs e);
    public delegate void UncheckedEventHandler(object sender, EventArgs e);
    #endregion

    #region : CheckBox :
    #region Template Parts...
    [TemplatePart(Name = CheckBox.CheckBoxLayoutRoot, Type = typeof(Grid))]
    [TemplatePart(Name = CheckBox.CanvasBorder, Type = typeof(Canvas))]
    [TemplatePart(Name = CheckBox.CanvasBackground, Type = typeof(Canvas))]
    [TemplatePart(Name = CheckBox.CanvasInnerBorder, Type = typeof(Canvas))]
    [TemplatePart(Name = CheckBox.CanvasInner, Type = typeof(Canvas))]
    [TemplatePart(Name = CheckBox.ImgCheck, Type = typeof(Image))]
    [TemplatePart(Name = CheckBox.TxtText, Type = typeof(TextBlock))]
    #endregion
    public class CheckBox : Control, IRibbonTabGroupItem
    {
        #region Events...
        /// <summary>
        /// Occurs when the checkbox is checked.
        /// </summary>
        public event CheckedEventHandler Checked;

        /// <summary>
        /// Occurs when the checkbox is unchecked.
        /// </summary>
        public event UncheckedEventHandler Unchecked;
        #endregion

        #region TemplatePart Names...
        public const string CheckBoxLayoutRoot = "CheckBoxLayoutRoot";
        public const string CanvasBorder = "CanvasBorder";
        public const string CanvasBackground = "CanvasBackground";
        public const string CanvasInnerBorder = "CanvasInnerBorder";
        public const string CanvasInner = "CanvasInner";
        public const string ImgCheck = "ImgCheck";
        public const string TxtText = "TxtText";
        #endregion

        #region TemplatePart Variables...
        internal Grid checkBoxLayoutRoot;
        internal Canvas canvasBorder;
        internal Canvas canvasBackground;
        internal Canvas canvasInnerBorder;
        internal Canvas canvasInner;
        internal Image imgCheck;
        internal TextBlock txtText;
        #endregion

        #region Class Variables...
        private bool templateApplied = false;
        private RibbonTabGroup parentRibbonTabGroup;
        #endregion

        #region Constants...
        private string CheckImageUri = "Images/RibbonCheck.png";
        #endregion

        #region Dependency Properties...
        #region LabelProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.CheckBox.Label dependency property.
        /// </summary>
        public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
                "Label",
                typeof(string),
                typeof(CheckBox),
                null);
        #endregion

        #region IsEnabledProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.CheckBox.IsEnabled dependency property.
        /// </summary>
        public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(
                "IsEnabled",
                typeof(bool),
                typeof(CheckBox),
                null);
        #endregion

        #region IsCheckedProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Input.CheckBox.IsChecked dependency property.
        /// </summary>
        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
                "IsChecked",
                typeof(bool),
                typeof(CheckBox),
                null);
        #endregion
        #endregion

        #region Dependency Property Changed Handlers...
        #endregion

        #region Constructor(s)...
        #region CheckBox
        public CheckBox() : base()
        {
            DefaultStyleKey = typeof(CheckBox);
            IsEnabled = true;
        }
        #endregion
        #endregion

        #region Event Handlers...
        #region checkBoxLayoutRoot_MouseEnter
        private void checkBoxLayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            if (IsEnabled)
            {
                //change the checkbox colour to represent the control being highlighted...
                canvasBorder.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBorderHighlighted);
                canvasBackground.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBackgroundHighlighted);
                canvasInnerBorder.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerBorderHighlighted);
                canvasInner.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerHighlighted);
            }
        }
        #endregion

        #region checkBoxLayoutRoot_MouseLeave
        private void checkBoxLayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            if (IsEnabled)
            {
                //change the checkbox colour to represent the control becoming unhighlighted...
                canvasBorder.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBorder);
                canvasBackground.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBackground);
                canvasInnerBorder.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerBorder);
                canvasInner.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInner);
            }
        }
        #endregion

        #region checkBoxLayoutRoot_MouseLeftButtonDown
        private void checkBoxLayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsEnabled)
            {
                //change the checkbox colour to represent the control being selected...
                canvasBorder.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBorderHighlightedAndSelected);
                canvasBackground.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBackgroundHighlightedAndSelected);
                canvasInnerBorder.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerBorderHighlightedAndSelected);
                canvasInner.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerHighlightedAndSelected);

                if (!IsChecked)
                {
                    IsChecked = true;
                    imgCheck.Source = new BitmapImage(new Uri(CheckImageUri, UriKind.Relative));
                }
                else
                {
                    IsChecked = false;
                }
            }
        }
        #endregion

        #region checkBoxLayoutRoot_MouseLeftButtonUp
        private void checkBoxLayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (IsEnabled)
            {
                //change the checkbox colour to represent the control being highlighted...
                canvasBorder.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBorderHighlighted);
                canvasBackground.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBackgroundHighlighted);
                canvasInnerBorder.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerBorderHighlighted);
                canvasInner.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerHighlighted);

                if (IsChecked)
                {
                    //raise the Checked event if there are attached event handlers...
                    if (Checked != null)
                        Checked(this, null);
                }
                else
                {
                    //remove the tick image...
                    imgCheck.Source = null;

                    //raise the Unchecked event if there are attached event handlers...
                    if (Unchecked != null)
                        Unchecked(this, null);
                }
            }
        }
        #endregion
        #endregion

        #region Methods...
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
            if (!templateApplied)
            {
                //get template children...
                checkBoxLayoutRoot = (Grid)GetTemplateChild(CheckBoxLayoutRoot);
                canvasBorder = (Canvas)GetTemplateChild(CanvasBorder);
                canvasBackground = (Canvas)GetTemplateChild(CanvasBackground);
                canvasInnerBorder = (Canvas)GetTemplateChild(CanvasInnerBorder);
                canvasInner = (Canvas)GetTemplateChild(CanvasInner);
                imgCheck = (Image)GetTemplateChild(ImgCheck);
                txtText = (TextBlock)GetTemplateChild(TxtText);

                //add mouse event handlers to the checkbox...
                checkBoxLayoutRoot.MouseEnter += new MouseEventHandler(checkBoxLayoutRoot_MouseEnter);
                checkBoxLayoutRoot.MouseLeave += new MouseEventHandler(checkBoxLayoutRoot_MouseLeave);
                checkBoxLayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(checkBoxLayoutRoot_MouseLeftButtonDown);
                checkBoxLayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(checkBoxLayoutRoot_MouseLeftButtonUp);

                //set the checkbox label if defined...
                if (!string.IsNullOrEmpty(Label))
                    txtText.Text = Label;

                //if the checkbox is checked by default - set the image source to the tick mark...
                if (IsChecked)
                    imgCheck.Source = new BitmapImage(new Uri(CheckImageUri, UriKind.Relative));

                //if the checkbox is disabled by default...
                if (!IsEnabled)
                {
                    //...change the checkbox colour to represent the control being disabled...
                    canvasBorder.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBorderDisabled);
                    canvasBackground.Background = Helper.GetBrush(SolidColorBrushes.CheckBoxBackgroundDisabled);
                    canvasInnerBorder.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerBorderDisabled);
                    canvasInner.Background = Helper.GetBrush(LinearGradientBrushes.CheckBoxInnerDisabled);

                    //...set the label colour to represent a disabled state...
                    txtText.Foreground = Helper.GetBrush(SolidColorBrushes.CheckBoxTextDisabled);
                }

                templateApplied = true;
            }
        }
        #endregion
        #endregion

        #region Properties...
        #region ParentRibbonTabGroup
        /// <summary>
        /// Gets or sets the parent RibbonTabGroup associated with this control...
        /// </summary>
        public RibbonTabGroup ParentRibbonTabGroup
        {
            get { return parentRibbonTabGroup; }
            set { parentRibbonTabGroup = value; }
        }
        #endregion

        #region Label
        /// <summary>
        /// Gets or sets the label for the checkbox...
        /// </summary>
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }
        #endregion

        #region IsChecked
        /// <summary>
        /// Gets or sets whether or not the checkbox is checked...
        /// </summary>
        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }
        #endregion

        #region IsEnabled
        /// <summary>
        /// Gets or sets whether or not the checkbox is enabled...
        /// </summary>
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }
        #endregion

        #region RibbonTabGroupItemRowSpan
        /// <summary>
        /// Gets the number or rows this control requires in a RibbonTabGroup...
        /// </summary>
        public RibbonTabGroupItemRowSpan RibbonTabGroupItemRowSpan
        {
            get { return RibbonTabGroupItemRowSpan.One; }
        }
        #endregion
        #endregion
    }
    #endregion
}